package com.fanvil.videoconference;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.ViewGroup;

import com.fanvil.videoconference.FvlSurfaceHolder;
import com.fanvil.videoconference.FvlSurfaceView;
import com.fanvil.videoconference.FvlLogger;

public class VideoSurfaceView extends FvlSurfaceView implements FvlSurfaceView.Callback {

    private static FvlLogger sLog = FvlLogger.getLogger("VideoSurfaceView", FvlLogger.VERBOSE);

    private FvlSurfaceHolder.Role mRole = FvlSurfaceHolder.Role.INVALID;
    private FvlSurfaceHolder.Role mPreRole = FvlSurfaceHolder.Role.INVALID;

    private boolean mCreated;

    private double mAspectRatio;

    public VideoSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addCallback(this);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VideoSurfaceView);
        int role = a.getInt(R.styleable.VideoSurfaceView_surfaceFor, FvlSurfaceHolder.Role.REMOTE.value());
        boolean zOrderMedia = a.getBoolean(R.styleable.VideoSurfaceView_zOrderMedia, true);
        a.recycle();

        setRole(FvlSurfaceHolder.Role.valueOf(role));

        if (zOrderMedia) {
            setZOrderMediaOverlay(true);
        } else {
            setZOrderOnTop(false);
        }
    }

    public void setRole(FvlSurfaceHolder.Role newRole) {
        if (mRole != newRole) {
            mRole = newRole;
            super.setRole(newRole);

            if (mCreated) {
                setSurfaceToCall();
            }
        }
    }

    public void updatePreRole(FvlSurfaceHolder.Role role) {
        mPreRole = role;
    }

    public FvlSurfaceHolder.Role getRole() {
        return mRole;
    }

    @Override
    public void requestAspectRatio(double aspectRatio) {
        super.requestAspectRatio(aspectRatio);
        mAspectRatio = aspectRatio;
    }

    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {
        sLog.d("surfaceCreated " + getRole());
        mCreated = true;
        setSurfaceToCall();
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        sLog.d("surfaceChanged " + getRole());
//        setSurfaceToCall();
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        sLog.d("surfaceDestroyed " + getRole());
        mCreated = false;
        mPreRole = FvlSurfaceHolder.Role.INVALID;
        setSurfaceToCall();
        mPreRole = FvlSurfaceHolder.Role.INVALID;
    }

    private void setSurfaceToCall() {
        if (mPreRole == FvlSurfaceHolder.Role.INVALID || mPreRole != mRole) {
            sLog.d(mCreated + " setSurfaceToCall:role=" + mRole + " preRole=" + mPreRole);
            mPreRole = mRole;
            FvlSurfaceHolder fvlHolder = getFvlSurfaceHolder();
            switch (mRole) {
                case LOCAL:
                    VideoManager.getInstance().setRemoteDisplay(0, fvlHolder);
                    break;
                case REMOTE:
                    VideoManager.getInstance().setRemoteDisplay(1, fvlHolder);
//                    MultiVideoManager.getInstance().setRemoteDisplay(fvlHolder);
                    break;
                case REMOTE2:
                    VideoManager.getInstance().setRemoteDisplay(2, fvlHolder);
//                    MultiVideoManager.getInstance().setRemoteDisplay(fvlHolder);
                    break;
            }
        }
    }

    @Override
    public void layout(int l, int t, int r, int b) {
        int width = r - l;
        int height = b - t;

        int widthSize = width;
        int heightSize = height;

        ViewGroup.LayoutParams params = getLayoutParams();
        if (params.width > 0) {
            // 固定大小，小的Surface布局方法
            if (mAspectRatio > 0) {
                heightSize = (int) (width / mAspectRatio);
            }
            b = t + heightSize;
        } else {
            // 大的Surface布局方法
            if (mAspectRatio > 0) {
                if (width > height * mAspectRatio) {
                    heightSize = (int) (width / mAspectRatio);
                } else {
                    widthSize = (int) (height * mAspectRatio);
                }
            }
            l = l + (width - widthSize) / 2;
            r = l + widthSize;
            t = t + (height - heightSize) / 2;
            b = t + heightSize;
        }
        // sLog.print(String.format("layout (%s-%s)-(%s-%s)", l, t, r, b));
        super.layout(l, t, r, b);
    }


}
